package org.csu.mypetstore.web.servlet;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Orders;
import org.csu.mypetstore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "ViewOrdersServlet", value = "/ViewOrdersServlet")
public class ViewOrdersServlet extends HttpServlet {
    private static String VIEW_ORDERS="/WEB-INF/jsp/order/ListOrders.jsp";
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session=request.getSession();
        OrderService service=new OrderService();
        List<Orders> ordersList=new ArrayList<Orders>();
        Orders orders=new Orders();
        orders.setUserid(((Account)session.getAttribute("accounts")).getUsername());
        orders.setShipaddr1(request.getParameter("shipaddress1"));
        orders.setShipaddr2(request.getParameter("shipaddress2"));
        orders.setShipcity(request.getParameter("shipcity"));
        orders.setShipstate(request.getParameter("shipstate"));
        orders.setShipzip(request.getParameter("shipzip"));
        orders.setShipcountry(request.getParameter("shipcountry"));
        orders.setBilladdr1(request.getParameter("billaddress1"));
        orders.setBilladdr2(request.getParameter("billaddress2"));
        orders.setBillcity(request.getParameter("billtocity"));
        orders.setBillstate(request.getParameter("billtostate"));
        orders.setBillzip(request.getParameter("billtozip"));
        orders.setBillcountry(request.getParameter("billtocountry"));
        orders.setTotalprice(Double.parseDouble(request.getParameter("totalprice")));

//        orders.setCourier(request.getParameter());
//        orders.setTotalprice(request.getParameter());
//        orders.setBilltofirstname(request.getParameter());
//        orders.setBilltolastname(request.getParameter());
//        orders.setShiptofirstname(request.getParameter());
//        orders.setShiptolastname(request.getParameter());
//        orders.setCreditcard(request.getParameter());
//        orders.setExprdate(request.getParameter());
//        orders.setCardtype(request.getParameter());
//        orders.setLocale(request.getParameter());
        orders.setItemid(request.getParameter("itemid"));

        service.insertOrder(orders);
        ordersList=service.getOrderByUsername(((Account)session.getAttribute("accounts")).getUsername() );
        session.setAttribute("ORDERLIST",ordersList);
        request.getRequestDispatcher(VIEW_ORDERS).forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
